package chaos.infinity.mixin.NoFloatCanExtend;

import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;
import net.minecraft.world.WorldView;
import net.minecraft.world.tick.ScheduledTickView;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(AbstractBlock.class)
abstract class UpdateOnPlace_Mixin {
    @Inject(method = "onBlockAdded", at = @At("TAIL"))
    protected void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify, CallbackInfo ci) {
        // 更新自身
        if (world instanceof ServerWorld) {
            ServerWorld serverWorld = (ServerWorld) world;
            // 触发方块自身的neighborupdata更新
            serverWorld.scheduleBlockTick(pos, (Block)(Object)this, 3);
        }
    }

    @Inject(method = "getStateForNeighborUpdate", at = @At("HEAD"))
    protected void getStateForNeighborUpdate(
            BlockState state,
            WorldView world,
            ScheduledTickView tickView,
            BlockPos pos,
            Direction direction,
            BlockPos neighborPos,
            BlockState neighborState,
            Random random,
            CallbackInfoReturnable<BlockState> cir
    ) {
        tickView.scheduleBlockTick(pos, (Block)(Object)this, 2);
    }
}
